const nodemailer = require('nodemailer')
const axios = require('axios')

const sendUser = 'lingshibococo@163.com'
const sendUserName = 'sweetQwQ'
const receiveUser = '3131912289@qq.com'

let transporter = nodemailer.createTransport({
    service: "163",  //  邮箱
    secure: true,    //  安全的发送模式
    auth: {
        user: sendUser, //  发件人邮箱
        pass: "ALXSVDIDDYCDCUDB"//  授权码
    }
})

function getDate() {
    const now = new Date()

    function getDay() {
        switch (now.getDay()) {
            case 0:
                return '星期天'
            case 1:
                return '星期一'
            case 2:
                return '星期二'
            case 3:
                return '星期三'
            case 4:
                return '星期四'
            case 5:
                return '星期五'
            case 6:
                return '星期六'
        }
    }

    return `今天是：${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${getDay()}`
}

function getProbability(val: number) {
    return Math.floor(Math.random() * 100) <= val
}

function sendEmail() {
    axios.get('http://wthrcdn.etouch.cn/weather_mini?citykey=101020100')
        .then(function (response) {
            const weather = {
                message: response.data.data.ganmao,
                city: response.data.data.city,
                high: '最' + response.data.data.forecast[0].high,
                low: '最' + response.data.data.forecast[0].low,
                wind: response.data.data.forecast[0].fengxiang + response.data.data.forecast[0].fengli.substring(9, 11),
            }
            const message = [['今天你学习了吗？', '今天你当懒狗了吗？', '还打算考研吗？', '跟老公说早上好了吗？'], ['不会还没起床吧？', '不会还在床上睡觉吧？', '不会还躺床上玩手机吧？', '不会还在躺尸吧？']]
            let mailOptions = {
                from: `${sendUserName}<${sendUser}>`,
                to: receiveUser,
                subject: "起床小助手",
                html: `<H2>${getDate()}</H2><h3>${weather.city}天气：${weather.high},${weather.low},${weather.wind}</h3><h4>${weather.message}</h4><p>${message[0][Math.floor(Math.random() * message[0].length)]}${message[1][Math.floor(Math.random() * message[1].length)]}</p>`
            }
            transporter.sendMail(mailOptions, (err, data) => {
                if (err) {
                    console.log(err);
                } else {
                    console.log(data);
                }
            })
            console.log('邮件发送成功！')
        })
        .catch(function (error) {
            console.log(error)
        })
}

setInterval(() => {
    const now = new Date()
    console.log(getProbability(10))
    console.log(`${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`)
    if (new Date().getHours() === 7 && new Date().getMinutes() === 0) {
        sendEmail()
    }
}, 60000)
